package com.home.controller;

import com.home.bean.CustomerBean;
import com.home.utils.SendEmail;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by Administrator on 2015/6/24.
 */
@Controller
public class CustomerController {
    @RequestMapping(value = "/customer")
    public void sendEmailServer(@ModelAttribute CustomerBean bean){
        StringBuilder sb = new StringBuilder("<div style='margin-left: 110px;width:45%;'><h4>姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名：");
        sb.append(bean.getName());sb.append("<br/>性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别：");
        sb.append(bean.getSex());sb.append("<br/>联系电话：");
        sb.append(bean.getPhone());sb.append("<br/>邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱：");
        sb.append(bean.getEmail());sb.append("<br/>联系&nbsp;QQ：");
        sb.append(bean.getQq());sb.append("<br/>说明原因：");
        sb.append(bean.getMsg());sb.append("</h4></div>");
        try {
            SendEmail.sendEmail("3245856068@qq.com", "合作申请", sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/download")
    public void download(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
        Resource re = new ClassPathResource("/basic.properties");
        Map map = new HashMap();
        try {
            Properties properties = PropertiesLoaderUtils.loadProperties(re);
            for(Object str:properties.keySet()){
                map.put(str,properties.get(str));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String ctxPath = request.getSession().getServletContext()
                .getRealPath("/");
        ctxPath = new File(ctxPath).getParent();
        String downLoadPath = ctxPath + map.get("downloadPath");
        downLoadPath = downLoadPath.replace("\\","/");
        System.err.println(downLoadPath);
        long fileLength = new File(downLoadPath).length();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment; filename="
                + new String("华西挂号.apk".getBytes("utf-8"), "ISO8859-1"));
        response.setHeader("Content-Length", String.valueOf(fileLength));
        bis = new BufferedInputStream(new FileInputStream(downLoadPath));
        bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            bos.write(buff, 0, bytesRead);
        }
        bis.close();
        bos.close();
    }
}
